package com.theory.common.exception;

import com.theory.common.result.R;
import com.theory.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 统一的异常处理器
 */
@Slf4j
//@Component  //Spring容易自动管理 不用加，RestControllerAdvice里面的ControllerAdvice里有
@RestControllerAdvice  //在controller层添加通知。如果使用@ControllerAdvice，则方法上需要添加@ResponseBody
public class UnifiedExceptionHandler {


    /**
     * 未定义异常
     */
    @ExceptionHandler(value = Exception.class)  //当controller中抛出Exception，则捕获
    public R handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.error();
    }

    /**
     * 特定异常  这样只能处理BadSqlGrammarException，如果想要单独处理某个异常就这样做
     */
//    @ExceptionHandler(BadSqlGrammarException.class)
//    public R handleBadSqlGrammarException(BadSqlGrammarException e){  // sql 出错的异常，需要引入jdbc的包才有这个类，否则爆红
//        log.error(e.getMessage(), e);
//        return R.result(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
//    }

    /**
     * 自定义异常 处理自定义异常类BusinessException的方法
     * @param e 自定义异常类，里面会携带code、message
     * @return 返回code和message给前台
     */
    @ExceptionHandler(BusinessException.class)
    public R handleBusinessException(BusinessException e) {
        log.error(e.getMessage(), e);
//        return R.error().message(e.getMessage()).code(e.getCode());  // 给R的code和message设置为异常类传过来的code和message
        return R.result(e.getCode(), e.getMessage());
    }

    /**
     * Controller上一层相关异常，这个阶段会出现各种异常，例如Http方法不支持、参数绑定、参数解析等等
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
//        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
        return R.result(ResponseEnum.SERVLET_ERROR);
    }
}
